package com.sun.opne;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import com.sun.base.UserActivity;
import com.sun.lotter.R;
import com.sun.net.LinkServer;
import com.sun.net.BaseDao.HttpListener;
import com.tools.MyLog;

public class OpneLotteryInfo extends UserActivity
{

	int pageSize = 10;
	int maxPageId = 1;
	int pageId = 1;
	private LinkServer lServer;
	private SimpleAdapter adapter;
	private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
	private ProgressBar progressBar;
	private ProgressBar mainProgressBar;
	private Button button;
	View view;

	@Override
	public void init()
	{
		noTitle = true;
		inflater.inflate(R.layout.openlotterinfo, mainLayout);
		view = mainLayout.findViewById(R.id.layout);
		view.setVisibility(View.GONE);
		progressBar = (ProgressBar) mainLayout.findViewById(R.id.progressBar);
		mainProgressBar = (ProgressBar) mainLayout.findViewById(R.id.progressBar1);
		ListView listView = (ListView) mainLayout.findViewById(R.id.listView);
		listView.setCacheColorHint(0);
		getData();
		adapter = new SimpleAdapter(this, dataList, R.layout.openlotterinfo_item, new String[]
		{ "issue", "numbers", "opentime" }, new int[]
		{ R.id.textView_issue, R.id.textView_numbers, R.id.textView_opentime });
		listView.setAdapter(adapter);
		button = (Button) mainLayout.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				pageId++;
				getData();
				button.setVisibility(View.INVISIBLE);
				progressBar.setVisibility(View.VISIBLE);
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener()
		{

			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				HashMap<String, String> map = (HashMap<String, String>) adapter.getItem(arg2);
				MyLog.v(map.get("issue") + "," + map.get("numbers") + "," + map.get("opentime"));
				Intent intent = new Intent();
				intent.setClass(OpneLotteryInfo.this, OpenLotterInfoDetail.class);
				intent.putExtra("expect", map.get("issue"));
				startActivity(intent);
			}
		});
	}

	private void getData()
	{
		lServer = new LinkServer(handler);
		lServer.SearchBeckerInfo(lotterId, pageSize, pageId, new HttpListener()
		{

			@Override
			public void onEvent(List<String> list)
			{
				mainProgressBar.setVisibility(View.GONE);
				view.setVisibility(View.VISIBLE);
				if (LinkServer.servertrue(list))
				{
					maxPageId = Integer.parseInt(list.get(3));
					for (int i = 0; i < (list.size() - 4) / 3; i++)
					{
						// 期号 开奖号 开奖时间
						HashMap<String, String> map = new HashMap<String, String>();
						map.put("issue", list.get(i * 3 + 4 + 0));
						map.put("numbers", list.get(i * 3 + 4 + 1));
						map.put("opentime", list.get(i * 3 + 4 + 2));
						dataList.add(map);
					}
				}
				else pageId--;
				progressBar.setVisibility(View.GONE);
				if (pageId < maxPageId)
				{
					button.setVisibility(View.VISIBLE);
				}
				else button.setVisibility(View.GONE);
				adapter.notifyDataSetChanged();
			}
		});
	}
}
